/*
 * SpaceServer.cpp
 *
 *  Created on: 1 mar 2010
 *      Author: Andreas
 */

#include "SpaceServer.h"
#include "SpaceApp.h"
#include "engine/util/string_manip.h"
class Bullet;

SpaceServer::SpaceServer(int port, SpaceApp *p)
 : Server(port)
{
	string lastKeys="";
	this->parent = p;
}

void SpaceServer::update(int elapsed) {

	vector<Player *> players = parent->getPlayers();

	/**** SEND (serverplayer) ****/
	posTimer += elapsed;
	if (posTimer >= 1000) {
		stringstream ss;
		ss << "10;" << players[0]->getXpos() << ";" << players[0]->getYpos() << ";" << players[0]->getAngle() << ";" << 0;
		send(ss.str());
		posTimer -= 1000;
	}
	/* Player keys */
	{
		stringstream ss;
		ss << "11";
		for (int i=0; i<4; i++) {
			ss << ";" << players[0]->getKeyDown(i);
		}
		ss << ";" << 0;
		if (lastKeys != ss.str()) {
			lastKeys = ss.str();
			send(lastKeys);
		}
	}
	/* bullet */
	while(players[0]->getSendBullet()>0){
		cout<<"megakill"<<endl;
		stringstream ss;
		float angle =players[0]->getBullets().back()->getAngle();
		int x =(int)players[0]->getBullets().back()->getXpos();
		int y =(int)players[0]->getBullets().back()->getYpos();
		float dx =players[0]->getBullets().back()->getDx();
		float dy =players[0]->getBullets().back()->getDy();
		int type =players[0]->getBullets().back()->getType();
		ss << "12;" << x << ";" << y << ";" << angle << ";" << dx << ";" << dy << ";" << type << "0" <<ss << ";" << 0;
		send(ss.str());
		players[0]->setSendBullet(players[0]->getSendBullet()-1);
	}

	/**** RECEIVE ****/
	while (true) {
		string msg = receive();
		if (msg == "") {
			break;
		}
		else if (msg == "new") {
			Player *p = new Player(Player::SHIP_BLUE);
			parent->addPlayer(p);

			// Let the client know he has been accepted and send number of players
			stringstream ss;
			ss << "1;" << parent->getPlayers().size();
			send(ss.str());
			break;
		}

		//cout << msg << endl;
		// Something received
		vector<string> results;
		results = split(msg, ";"); /* Split the message by ';' */

		// Player pos
		if (results[0] == "10") {
			int x = atoi(results[1].c_str());
			int y = atoi(results[2].c_str());
			float angle = atof(results[3].c_str());
			int user = findUser(atoi(results[4].c_str())) + 1; /* +1 to sync send_packets with the player vector */

			Player *p = players[user];
			p->moveTo(x, y);
			p->setAngle(angle);

			// Vidarebefordra till alla andra
			for (int i=1; i<players.size(); i++) {
				if (i != user) {
					stringstream ss;
					ss << "10;" << results[1] << ";" << results[2] << ";" << results[3] << ";" << user;
					send(ss.str(), i-1);
				}
			}
		}
		// Player keys
		else if (results[0] == "11") {
			int keys[5];
			for (int i=0; i<4; i++) {
				keys[i] = atoi(results[i+1].c_str());
			}
			keys[4] = 0;
			int user = findUser(atoi(results[5].c_str())) + 1;
			players[user]->setKeysDown(keys);

			// Vidarebefordra till alla andra
			for (int i=1; i<players.size(); i++) {
				if (i != user) {
					stringstream ss;
					ss << "11";
					for (int i=0; i<4; i++) {
						ss << ";" << results[i+1];
					}
					ss << ";" << user;
					send(ss.str(), i-1);
				}
			}
		}
		// Shot fired
		else if (results[0] == "12"){
			cout << msg << endl;
			int x = atoi(results[1].c_str());
			int y = atoi(results[2].c_str());
			float angle = atof(results[3].c_str());
			float dx = atof(results[4].c_str());
			float dy = atof(results[5].c_str());
			int type = atoi(results[6].c_str());
			int user = findUser(atoi(results[7].c_str())) + 1;
			Player *p = players[user];
			p->addBullet(x,y,angle,dx,dy,type);

			// Vidarebefordra till alla andra
			for (int i=1; i<players.size(); i++) {
				if (i != user) {
					stringstream ss;
					ss << "12;" << results[1] << ";" << results[2] << ";" << results[3] << ";" << results[4] << ";" << results[5] << ";" << results[6] << ";" << user;
					send(ss.str(), i-1);
				}
			}
		}


	}

}

